<?php

class Admin_Search_Pickup extends Controller
{
	var $data=array();
	
	public function __construct()
	{
		parent::Controller();
        include("password_protect.php");
		$this->load->model('Admin_Input_Model');
		$this->load->model('Admin_Quotation_Model');
		
		$this->data['warning']="";
		$this->data['success']="";
		$this->data['quote_list']=null;	

		$this->data['city']="";
		$this->data['province_state']="";
		$this->data['country']="";
//		$this->data['province_list']=$this->get_province_list();
		$this->data['your_number']="";
		$this->data['province_exists']="";
		$this->data['city_exists']="";
		$this->data['search_city_list']=array();
		$this->data['search_province_list']=array();
		$this->data['search']="";
		$this->data['search_province']="";
		$this->data['hidden_province_id']=-1;
		$this->data['search_city_id']=-1;

		$this->data['start_date']="";
		$this->data['end_date']="";
		
		$this->data['other_comment']="";
		$this->data['shipdest_comment']="";
		
		$this->data['email']="";
		$this->data['requester_name']="";
		$this->data['requesting_comp_name']="";
		$this->data['requesting_comp_name']="";
		$this->data['requester_phone']="";
		$this->data['designator_id']=-1;
		$this->data['designator_title']="";
		$this->data['city_list']="";

		$this->data['pickup_time']="";
		$this->data['pickup_time_period']="";
		$this->data['dock_closed_time']="";
		$this->data['dock_closed_time_period']="";
		
		$this->data['pickup_date']="";
		$this->data['pickup_contact_name']="";
		$this->data['pickup_address']="";
		$this->data['pickup_city']="";
		$this->data['pickup_province']="";
		$this->data['pickup_country']="";
		$this->data['pickup_p_code']="";
		$this->data['pickup_fax']="";
		$this->data['pickup_company_name']="";
		$this->data['pickup_phone_number']="";

		$this->data['destination_contact_name']="";
		$this->data['destination_address']="";
		$this->data['destination_p_code']="";
		$this->data['destination_city']="";
		$this->data['destination_province']="";
		$this->data['destination_country']="";
		$this->data['destination_fax']="";
		$this->data['destination_company_name']="";
		$this->data['destination_phone_number']="";
		
		$this->data['goods_description']="";
		$this->data['total_pieces']="";
		$this->data['total_weight']="";
		$this->data['hand_inst']="";
		$this->data['is_hazardous']=-1;

		$this->data['designator_id']=-1;
	}
	
	function index()
	{
		if($this->input->post('search_quote'))
		{
			$start = trim($this->input->post('start_date'));
			$end = trim($this->input->post('end_date'));
			
			if(strcmp($start,"")!=0)
			{
				$month = strtok($start, "/");
				$date = strtok("/");
				$year = strtok("/");
			
				$start = $year."-".$month."-".$date;
			}
			else
			{
				$start = "0000-00-00";
			}
			

			if(strcmp($end,"")!=0)
			{
				$month = strtok($end, "/");
				$date = strtok("/");
				$year = strtok("/");
				
				$end = $year."-".$month."-".$date;
			}
			else 
			{
				$end="9999-01-01";
			}
			
			$this->data['start_date']=$start;
			$this->data['end_date']=$end;
			
			$this->data['quote_list'] = $this->Admin_Input_Model->list_quotation_pickup($start, $end);

			if(count($this->data['quote_list'])==0)
				$this->data['warning']="No quotation found between ".$this->data['start_date']." and ".$this->data['end_date'];
			
			$this->load->view("admin_search_pickup",$this->data);

		}
		
		else if($this->input->post('quotation_detail'))
		{
			$id = $this->input->post('hidden_quote_id');

			$this->read_quotation($id);
		}
		else 
			$this->load->view("admin_search_pickup",$this->data);
	}
	
	function read_quotation($id)
	{
		$quotation=$this->Admin_Quotation_Model->read_quotation($id);

		if($quotation)
		{
			$this->data['requester_name']=$quotation->requester_name;
	//		$this->data['requesting_comp_name']=$quotation->requesting_comp_name;
			$this->data['your_number']=$quotation->phone;
			$this->data['email']=$quotation->email;
			$this->data['requester_phone']=$quotation->phone;
			$this->data['designator_id']=$quotation->designation_id;
			$this->data['designator_title']=$this->Admin_Quotation_Model->get_designation_id($this->data['designator_id']);
			$this->data['pickup_date']=$quotation->pickup_date;
			$this->data['goods_description']=$quotation->goods_description;
			$this->data['other_comment']=$quotation->other_comment;
			$this->data['shipdest_comment']=$quotation->shipping_desctination_comment;
			$this->data['hand_inst']=$quotation->handling_instruction;
			$this->data['pickup_date']=$quotation->pickup_date;
			$this->data['pickup_time']=$quotation->pickup_time;
			$this->data['dock_closed_time']=$quotation->dock_closing_time;
			$this->data['total_weight']=$quotation->total_weight;
			$this->data['is_hazardous']=$quotation->is_hazardous;
			$this->data['total_pieces']=$quotation->pieces;
			
			$address=$this->Admin_Quotation_Model->read_address($quotation->origin_address_id);
			$city=$this->Admin_Quotation_Model->get_city($address->city_id);
			$this->data['pickup_city']=$city->city_name;

			$province=$this->Admin_Quotation_Model->get_province($address->province_id);
//			$this->data['pickup_province']=$province->state_province_name;
			$this->data['pickup_contact_name']=$address->name;
//			$this->data['pickup_country']=$province->country_name;
			$this->data['pickup_address']=$address->street;
			$this->data['pickup_p_code']=$address->postal_code;
			$this->data['pickup_fax']=$address->fax;
			$this->data['pickup_company_name']=$address->company_name;
			$this->data['pickup_phone_number']=$address->phone;

			$address=$this->Admin_Quotation_Model->read_address($quotation->destination_address_id);
			$city=$this->Admin_Quotation_Model->get_city($address->city_id);
			$this->data['destination_city']=$city->city_name;

			$province=$this->Admin_Quotation_Model->get_province($address->province_id);
//			$this->data['destination_country']=$province->country_name;
//			$this->data['destination_province']=$province->state_province_name;
			$this->data['destination_contact_name']=$address->name;;
			$this->data['destination_address']=$address->street;
			$this->data['destination_p_code']=$address->postal_code;
			$this->data['destination_fax']=$address->fax;
			$this->data['destination_company_name']=$address->company_name;
			$this->data['destination_phone_number']=$address->phone;
		}
		$this->load->view("admin_quotation",$this->data);
	}

}